package com.ruoyi.common.utils;

public class UserAgentUtil {
    private String userAgentString;
    private String browser;
    private String os;


    public UserAgentUtil(String userAgentString) {
        String userAgentLowercaseString = userAgentString == null ? "" : userAgentString.toLowerCase();
        this.userAgentString = userAgentLowercaseString;
        parseOs();
        parseBrowser();
    }

    private void parseOs() {
        if (this.userAgentString != "") {
            if (this.userAgentString.indexOf("android") > 0) {
                this.os = "android";
            }else if (this.userAgentString.indexOf("xbox") > 0) {
                this.os = "Xbox";
            }  else if (this.userAgentString.indexOf("windows") > 0) {
                this.os = "Windows";
            } else if (this.userAgentString.indexOf("iphone") > 0) {
                this.os = "iPhone";
            } else if (this.userAgentString.indexOf("ios") > 0) {
                this.os = "ios";
            } else if (this.userAgentString.indexOf("mac os") > 0) {
                this.os = "Mac OS";
            } else if (this.userAgentString.indexOf("kindle") > 0) {
                this.os = "Kindle";
            } else if (this.userAgentString.indexOf("symbian") > 0) {
                this.os = "Symbian";
            }else if (this.userAgentString.indexOf("linux") > 0) {
                this.os = "Linux";
            }else if (this.userAgentString.indexOf("curl") > 0) {
                this.os = "curl";
            }else{
                this.os = "UKOWN";
            }
        }else{
            this.os = "UKOWN";
        }
    }

    private void parseBrowser(){
        if (this.userAgentString != "") {
            if (this.userAgentString.indexOf("outlook") > 0) {
                this.browser = "Outlook";
            }else if (this.userAgentString.indexOf("internet explorer") > 0) {
                this.browser = "Internet Explorer";
            }  else if (this.userAgentString.indexOf("ie mobile") > 0) {
                this.browser = "IE Mobile";
            } else if (this.userAgentString.indexOf("microsoft edge") > 0) {
                this.browser = "Microsoft Edge";
            } else if (this.userAgentString.indexOf("chrome") > 0) {
                this.browser = "Chrome";
            } else if (this.userAgentString.indexOf("firefox") > 0) {
                this.browser = "Firefox";
            } else if (this.userAgentString.indexOf("safari") > 0) {
                this.browser = "Safari";
            } else if (this.userAgentString.indexOf("apple") > 0) {
                this.browser = "Apple WebKit";
            }else if (this.userAgentString.indexOf("vivaldi") > 0) {
                this.browser = "Vivaldi";
            }else if (this.userAgentString.indexOf("robot") > 0||this.userAgentString.indexOf("spider")>0) {
                this.browser = "Spider";
            }else if (this.userAgentString.indexOf("mozilla") > 0) {
                this.browser = "Mozilla";
            }else if (this.userAgentString.indexOf("curl") > 0) {
                this.browser = "curl";
            }else{
                this.browser = "UKOWN";
            }
        }else{
            this.browser = "UKOWN";
        }
    }

    public String getBrowser() {
        return browser;
    }

    public void setBrowser(String browser) {
        this.browser = browser;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }
}
